package ace.module.common.core.test.core.service;

import ace.cmp.core.constants.CoreConstant;
import ace.cmp.id.generator.api.LongIdGenerator;
import ace.module.common.api.constant.CommonConstants;
import ace.module.common.api.constant.SystemRoleConstants;
import ace.module.common.core.test.core.constant.TestConstant;
import ace.module.common.core.test.core.dao.entity.MmsApplication;
import ace.module.common.core.test.core.dao.entity.MmsAuthor;
import ace.module.common.core.test.core.dao.entity.MmsBook;
import ace.module.common.core.test.core.dao.repository.MmsApplicationRepository;
import ace.module.common.core.test.core.dao.repository.MmsAuthorRepository;
import ace.module.common.core.test.core.dao.repository.MmsBookRepository;
import java.util.Arrays;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author caspar
 * @date 2023/8/26 17:01
 */
@Component
@AllArgsConstructor
@Slf4j
public class InitDataService {

  private final MmsAuthorRepository mmsAuthorRepository;
  private final MmsBookRepository mmsBookRepository;
  private final MmsApplicationRepository mmsApplicationRepository;
  private final LongIdGenerator idGenerator;

  @Transactional(rollbackFor = Exception.class)
  public void init() {

    MmsApplication mmsApplication1 = MmsApplication.builder()
        .id(TestConstant.DEFAULT_APPLICATION_ID_1)
        .name(TestConstant.DEFAULT_APPLICATION_ID_1.toString())
        .status(CommonConstants.STATUS_NORMAL)

        .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
        .createTime(System.currentTimeMillis())
        .updateTime(System.currentTimeMillis())
        .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
        .build();

    Long appId1 = mmsApplication1.getId();
    List<MmsAuthor> mmsAuthors =
        Arrays.asList(this.createAuthor(1L, appId1),
            this.createAuthor(2L, appId1),
            this.createAuthor(3L, appId1)
        );
    List<MmsBook> mmsBooks =
        Arrays.asList(
            this.createBook(1L, appId1, 1L),
            this.createBook(2L, appId1, 1L),
            this.createBook(3L, appId1, 2L),
            this.createBook(4L, appId1, 3L),
            this.createBook(5L, appId1, 2L));

    mmsApplicationRepository.insert(mmsApplication1);
    mmsAuthorRepository.insert(mmsAuthors);
    mmsBookRepository.insert(mmsBooks);

    MmsApplication mmsApplication2 = MmsApplication.builder()
        .id(TestConstant.DEFAULT_APPLICATION_ID_2)
        .outId(TestConstant.DEFAULT_APPLICATION_2_OUT_ID.toString())
        .outType(SystemRoleConstants.DEFAULT)
        .name(TestConstant.DEFAULT_APPLICATION_ID_2.toString())
        .status(CommonConstants.STATUS_NORMAL)

        .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
        .createTime(System.currentTimeMillis())
        .updateTime(System.currentTimeMillis())
        .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
        .build();

    long stepId = 10L;
    Long appId2 = mmsApplication2.getId();
    mmsAuthors =
        Arrays.asList(this.createAuthor(stepId + 1L, appId2),
            this.createAuthor(stepId + 2L, appId2),
            this.createAuthor(stepId + 3L, appId2)
        );
    mmsBooks =
        Arrays.asList(
            this.createBook(stepId + 1L, appId2, stepId + 1L),
            this.createBook(stepId + 2L, appId2, stepId + 1L),
            this.createBook(stepId + 3L, appId2, stepId + 2L),
            this.createBook(stepId + 4L, appId2, stepId + 3L),
            this.createBook(stepId + 5L, appId2, stepId + 2L));

    mmsApplicationRepository.insert(mmsApplication2);
    mmsAuthorRepository.insert(mmsAuthors);
    mmsBookRepository.insert(mmsBooks);
  }

  private MmsAuthor createAuthor(Long id, Long appId) {
    return MmsAuthor.builder()
        .id(id)
        .appId(appId)
        .name("author" + id.toString())
        .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
        .createTime(System.currentTimeMillis())
        .updateTime(System.currentTimeMillis())
        .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
        .build();
  }

  private MmsBook createBook(Long id, Long appId, Long authorId) {
    return MmsBook.builder()
        .id(id)
        .appId(appId)
        .name("book" + id.toString())
        .authorId(authorId)

        .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
        .createTime(System.currentTimeMillis())
        .updateTime(System.currentTimeMillis())
        .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
        .build();
  }
}
